package main

import (
	"crypto/sha256"
)

type Block struct {
	// 前置hash
	PreHash []byte

	// 当前hash
	Hash []byte

	// data
	Data []byte
}

// 创建区块
func NewBlock(data string, preHash []byte) *Block {
	block := Block{
		PreHash: preHash,
		Hash:    []byte{},
		Data:    []byte(data),
	}
	block.SetHash()
	return &block
}

// 设置 hash
func (b *Block) SetHash() {
	blockInfo := append(b.PreHash, b.Data...)
	hash := sha256.Sum256(blockInfo)
	b.Hash = hash[:]
}
